package com.github.data_manage.dao

import com.github.data_manage.domain.po.ContrastNavData
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime

/**
 *@author CHN
 *@since 2024-08-10 14:48
 */
interface ContrastNavDataDao : BaseDao<ContrastNavData> {

    @Query("select f from ContrastNavData f where f.contrastId = :contrastId order by f.time desc ")
    fun findAllByContrastId(@Param("contrastId") contrastId: Int): List<ContrastNavData>

    fun findAllByContrastIdAndTimeLessThan(contrastId: Int, time: LocalDate): List<ContrastNavData>

    @Query("select f from ContrastNavData f where f.contrastId = :contrastId and f.time <= :lastMonth order by f.time desc limit 1")
    fun findLastNavDataByContrastId(@Param("contrastId") contrastId: Int, @Param("lastMonth") lastMonthEndTime: LocalDate): ContrastNavData?

    fun findContrastNavDataByContrastIdAndTime(contrastId: Int, time: LocalDate): ContrastNavData?

    @Modifying
    @Transactional
    fun deleteAllByContrastId(contrastId: Int)

    @Query("select f from ContrastNavData f where f.contrastId = :contrastId and f.time <= :atEndOfMonth order by f.time ")
    fun findAllByContrastIdAndTime(contrastId: Int, atEndOfMonth: LocalDate): List<ContrastNavData>

    fun findAllByContrastIdIn(contrastIds: List<Int>): List<ContrastNavData>

    fun findAllByContrastIdIsAndTimeBetween(contrastId: Int, startTime: LocalDate, endTime: LocalDate): List<ContrastNavData>
}
